﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace _9
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private async void button1_Click(object sender, EventArgs e)
        {
            string url = textBoxUrl.Text;

            // 判断 URL 是否为空
            if (string.IsNullOrWhiteSpace(url))
            {
                MessageBox.Show("请输入有效的URL。");
                return;
            }
            using (HttpClient client = new HttpClient())
            {
                try
                {
                    // 发送 GET 请求
                    HttpResponseMessage response = await client.GetAsync(url);
                    response.EnsureSuccessStatusCode();
                    string responseBody = await response.Content.ReadAsStringAsync();
                    //实在没找到有邮箱和手机号的页面

                    string script = @"
                    var phonePattern = /\b1\d{10}\b/g;
                    var emailPattern = /\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b/g;
                    var content = document.body.innerHTML;
                    content = content.replace(phonePattern, '<span style=""color: blue;"">$&</span>');//蓝色标注手机号
                    content = content.replace(emailPattern, '<span style=""color: red;"">$&</span>');//红色标注邮箱
                    document.body.innerHTML = content;
                ";
                    // 自带的webBrowser浏览器比较老很多功能都加载不了，所有js加载错误都选否就可以了
                    webBrowser1.DocumentText = responseBody;
                    webBrowser1.DocumentCompleted += (s, args) =>
                    {
                        webBrowser1.Document.InvokeScript("eval", new object[] { script });
                    };
                }
                catch (HttpRequestException ex)
                {
                    MessageBox.Show("HTTP 请求错误：" + ex.Message);
                }
            }
        }
        
    }
}